class UtilDB {
  constructor(store, ...index) {
    this.store = store;
    this.index = index;
  }
  /**
     * 打开创建数据库 返回promise
     * @param {DB名字} name 
     * @param {版本号} version 
     */
  openDB(name, version = 1, keypath) {
    let indexDB =
      window.indexedDB ||
      window.mozIndexedDB ||
      window.webkitIndexedDB ||
      window.msIndexedDB;
    let request = indexDB.open(name, version);
    const pormise = new Promise((resolve, reject) => {
      request.onerror = e => {
        console.log("连接indexDB数据库失败");
        console.error(e.currentTarget.error.message);
        reject(e.currentTarget.error.message);
      };
      request.onsuccess = e => {
        console.log("连接indexDB数据库成功");
        this.db = e.target.result;
        resolve(this.db);
      };
      request.onupgradeneeded = e => {
        let db = e.target.result;
        if (!db.objectStoreNames.contains(this.store)) {
          let store = db.createObjectStore(this.store, {
            keypath: keypath,
            autoIncrement: true
          });
          this.index.forEach(item => {
            if (Object.prototype.toString.call({}) == "[object Object]") {
              store.createIndex(item.name + "", item.name + "", item.options);
            } else {
              store.createIndex(item + "", item + "", { unique: false });
            }
          });
        }
      };
    });
    return pormise;
  }
  commonCrud(way) {
    let normal = this.store + "";
    let tx = this.db.transaction(normal, way);
    let store = tx.objectStore(normal);
    return store;
  }
  addData(val, callback) {
    let req = this.commonCrud("readwrite");
    req = req.add(val);
    req.onsuccess = e => {
      console.info("addData", "添加数据成功");
      return callback(e.target);
    };
    req.onerror = e => {
      console.error("add Error", "添加数据失败");
      return callback(e.target);
    };
  }

  putData(val, callback) {
    let req = this.commonCrud("readwrite");

    req = req.put(val, val.userName);

    req.onsuccess = e => {
      console.log("修改数据成功");

      return callback(e.target);
    };

    req.onerror = e => {
      console.log("修改数据失败");

      return callback(e.target);
    };
  }
  getData(val, callback) {
    let req = this.commonCrud("readonly");
    req = req.get(val);
    req.onsuccess = e => {
      if (e.target.result) {
        console.log("获取数据成功");
        return callback(e.target.result);
      }
    };
    req.onerror = e => {
      console.log("获取数据失败");
      return callback(e);
    };
  }
  deleteData(val, callback) {
    let req = this.commonCrud("readwrite");
    req = req.delete(val);
    req.onerror = e => {
      console.log("系统错误");
      return callback(e.target);
    };
    req.onsuccess = e => {
      console.log("执行成功");
      return callback(e.target);
    };
  }
  getSignleData(name, callback) {
    console.log("获取单条信息");
    let req = this.commonCrud("readonly");
    let index = req.index("userName");
    req = index.get(name);
    req.onsuccess = e => {
      console.log("获取单条数据成功");
      return callback(e.target.result);
    };
    req.onerror = e => {
      console.log("获取单条数据成功");
      return callback(e.target.result);
    };
  }
  deleteAssignData(userName, callback) {
    let req = this.commonCrud("readwrite");
    req = req.openCursor();
    req.onerror = e => {
      console.error("query:error", "查询使用游标时出错");
      return callback(e.error.name);
    };
    req.onsuccess = e => {
      let cursor = e.target.result;
      if (cursor) {
        if (cursor.userName == userName) {
          cursor.delete();
        }
        cursor.continue();
      } else {
        return callback(cursor);
      }
    };
  }
  getAllData(callback) {
    let req = this.commonCrud("readwrite");
    let result = [];
    req = req.openCursor();
    req.onerror = e => {
      console.error("query error", e.error);
      return callback(e.target);
    };
    req.onsuccess = e => {
      let cursor = e.target.result;
      if (cursor) {
        result.push(cursor.value);
        cursor.continue();
      } else {
        return callback(result);
      }
    };
  }
  onError(calback) {
    return callback(event);
  }
  onSuccess(calback) {
    return callback(event);
  }
  onComplete(calback) {
    return callback(event);
  }
  /**
   * 关闭数据库
   * @param {数据库} db 
   */
  closeDB(db) {
    db.close();
  }
  /**
   * 删除数据库
   * @param {数据库名称} name 
   */
  deleteDB(name) {
    indexedDB.deleteDatabase(name);
  }
}
export default UtilDB;
